package com.atguigu.sugar.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.sugar.bean.KW;
import com.atguigu.sugar.bean.TrafficUvCt;
import com.atguigu.sugar.service.TrafficService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @auther yuzehua
 * @date 2022/10/27 - 16:40
 */
@RestController
public class SugarController {
    @Autowired
    TrafficService trafficService;

    @RequestMapping("/sugar/kw")
    public String kw(Integer date) {
        // 如果是 null, 表示要查的是当天
        if (date == null) {
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }


        List<KW> list = trafficService.kw(date);
        for (KW kw : list) {
            System.out.println("kw>>>>>"+kw);
        }
        JSONObject result = new JSONObject();

        result.put("status", 0);
        result.put("msg", "");

        JSONArray data = new JSONArray();

        for (KW kw : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", kw.getKeyword());
            obj.put("value", kw.getScore());
            data.add(obj);
        }

        result.put("data", data);
        System.out.println(result.toJSONString());
        return result.toJSONString();
    }

    @RequestMapping("/sugar/uvCt")
    public String selectUvCt(Integer date){
        // 如果是 null, 表示要查的是当天
        if (date == null) {
            date = Integer.valueOf(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        List<TrafficUvCt> list = trafficService.selectUvCt(date);
        for (TrafficUvCt trafficUvCt : list) {
            System.out.println(trafficUvCt);
        }

        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONObject data = new JSONObject();

        ArrayList<String> categories = new ArrayList<>();
        ArrayList<Integer> series = new ArrayList<>();

        for (TrafficUvCt trafficUvCt : list) {
            categories.add(trafficUvCt.getCh());
            series.add(trafficUvCt.getUvCt());
        }

        for (String category : categories) {
            System.out.println(category);
        }

        data.put("categories",categories);
        data.put("series",series);
        

        result.put("data",data);

        return result.toJSONString();
    }
}
